﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.RemoteX509AsymmetricSecurityKey
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.IdentityModel.Tokens;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace Microsoft.InfoCards
{
  internal class RemoteX509AsymmetricSecurityKey : X509AsymmetricSecurityKey, IDisposable
  {
    private X509Certificate2 m_cert;
    private object m_sync;
    private bool m_disposed;

    public RemoteX509AsymmetricSecurityKey(X509Certificate2 cert)
      : base(cert)
    {
      this.m_cert = cert;
      if (!(this.m_cert.PrivateKey is RemoteCryptoRsaServiceProvider))
        throw InfoCardTrace.ThrowHelperError((Exception) new NotSupportedException());
      this.m_sync = new object();
    }

    public override AsymmetricAlgorithm GetAsymmetricAlgorithm(
      string algorithmUri,
      bool privateKey)
    {
      if (!this.IsSupportedAlgorithm(algorithmUri))
        throw InfoCardTrace.ThrowHelperError((Exception) new NotSupportedException());
      return base.GetAsymmetricAlgorithm(algorithmUri, privateKey);
    }

    public override AsymmetricSignatureDeformatter GetSignatureDeformatter(
      string algorithmUri)
    {
      if (!this.IsSupportedAlgorithm(algorithmUri))
        throw InfoCardTrace.ThrowHelperError((Exception) new NotSupportedException());
      return base.GetSignatureDeformatter(algorithmUri);
    }

    public override AsymmetricSignatureFormatter GetSignatureFormatter(
      string algorithmUri)
    {
      if (!this.IsSupportedAlgorithm(algorithmUri))
        throw InfoCardTrace.ThrowHelperError((Exception) new NotSupportedException());
      RemoteAsymmetricSignatureFormatter signatureFormatter = new RemoteAsymmetricSignatureFormatter();
      signatureFormatter.SetKey(this.m_cert.PrivateKey);
      return (AsymmetricSignatureFormatter) signatureFormatter;
    }

    public override bool IsSupportedAlgorithm(string algorithmUri)
    {
      switch (algorithmUri)
      {
        case "http://www.w3.org/2000/09/xmldsig#rsa-sha1":
        case "http://www.w3.org/2001/04/xmlenc#rsa-1_5":
        case "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p":
          return base.IsSupportedAlgorithm(algorithmUri);
        default:
          return false;
      }
    }

    void IDisposable.Dispose()
    {
      if (this.m_disposed)
        return;
      lock (this.m_sync)
      {
        if (this.m_disposed)
          return;
        this.m_disposed = true;
        ((IDisposable) this.m_cert.PrivateKey)?.Dispose();
        ((IDisposable) this.m_cert.PublicKey.Key)?.Dispose();
      }
    }
  }
}
